package cn.edu.ujn.lishlab3.controller;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.lishlab3.dao.Customer;
import cn.edu.ujn.lishlab3.dao.User;
import cn.edu.ujn.lishlab3.service.IUserService;
import cn.edu.ujn.util.Page;

@Controller
public class UserController {

	@Autowired
	private IUserService userService;
//
	
	@GetMapping("/login.action")
	public String toLogin() {
		return "login";
	}

	@RequestMapping("/toCustomer")
	public String toCustomer() {
		return "customer";
	}
//
	
	@RequestMapping("/logout.action")
	public String logout(HttpSession session) {
		// 清除Session
		session.invalidate();
		// 重定向到登录页面的跳转方法
		return "redirect:login.action";
	}

	@PostMapping("/login.action")
	public String login(String usercode, String password, Model model, HttpSession session) {
		User user = userService.findUserByCodeAndPassword(usercode, password);
		if (user != null) {
			session.setAttribute("USER_SESSION", user);
//			return "customer";
			return "redirect:customer/list.action";
		}
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
		return "login";
	}

	@RequestMapping(value = "/user/getUser.action")
	@ResponseBody
	public User getUser(Integer id) {
		User user = userService.selectByPrimaryKey(id);
		return user;
	}

	/**
	 * 创建用户(测试正常)
	 */
	@RequestMapping(value = "/user/create.action")
	@ResponseBody
	public String userCreate(User user, Integer user_id, String user_code, String user_name, String user_password,
			Integer user_state, HttpSession session) {
//		// 获取Session中的当前用户信息
//		User user1 = (User) session.getAttribute("USER_SESSION");
		if (!StringUtils.isBlank(user_code) && !StringUtils.isBlank(user_name) && !StringUtils.isBlank(user_password)) {
			if (userService.selectByUserCode(user_code) != null) {
				return "FAIL1"; // 错误1：账号已存在
			}
			user.setUser_id(user_id);
			user.setUser_code(user_code);
			user.setUser_name(user_name);
			user.setUser_password(user_password);
			user.setUser_state(user_state);
			// 执行Service层中的创建方法，返回的是受影响的行数
			int rows = userService.insertSelective(user);
			if (rows > 0) {
				return "OK";
			} else {
				return "FAIL2"; // 错误2：创建失败
			}
		}
		return "FAIL3";		//错误3：有空项
	}

	/**
	 * 用户列表
	 */

	@RequestMapping(value = "/user/list.action")
	public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows,
			Integer user_id, String user_code, String user_name, String user_password, Integer user_state,
			Model model) {
		// 条件查询所有用户
		Page<User> users = userService.findUserList(page, rows, user_id, user_code, user_name, user_password,
				user_state);
		// 添加参数
		model.addAttribute("page", users);
		model.addAttribute("user_id", user_id);
		model.addAttribute("user_code", user_code);
		model.addAttribute("user_name", user_name);
		model.addAttribute("user_password", user_password);
		model.addAttribute("user_state", user_state);
		return "user";
	}

	/**
	 * 删除客户
	 */
	@RequestMapping("/user/delete.action")
	@ResponseBody
	public String userDelete(Integer id) {
		int rows = userService.deleteByPrimaryKey(id);
		if (rows > 0) {
			return "OK";
		} else {
			return "FAIL";
		}
	}

	
	//更新客户
		@RequestMapping("/user/update.action")
		@ResponseBody
		public String userUpdate(User user) {
			System.out.println(user);
		    int rows = userService.updateUser(user);
		    if(rows > 0){
		        return "OK";
		    }else{
		        return "FAIL";
		    }
		}

}
